package com.syxp.pda.action.base;

import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.util.Operation;
import com.syxp.pda.core.web.PagingBean;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.User;
import com.syxp.pda.entity.system.Role;
import com.syxp.pda.service.base.UserService;

public class UserAction extends BaseAction {
	@Resource
	private UserService userService;
	
	private int userID;
	
	private int departmentID ;
	
	private User user;
	
	private String password;
	
	private int start ;
	
	private int limit ;
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public UserService getUserService() {
		return userService;
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public int getUserID() {
		return userID;
	}
	
	public void setUserID(int userID) {
		this.userID = userID;
	}
	
	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}
	
	
	public int getDepartmentID() {
		return departmentID;
	}

	public void setDepartmentID(int departmentID) {
		this.departmentID = departmentID;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	public String save(){ //保存
		try{
			System.out.println("professID   " + user.getProfess().getProfessID()) ;
			//user.setPassWord("88888888") ;
			User s= userService.addService(user) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("id", String.valueOf(user.getUserID())) ;
			jobj.addProperty("name", user.getUserName()) ;
			jobj.addProperty("success", true) ;
			this.jsonString=super.gson.toJson(jobj) ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String load() {  //加载所有数据
		try{
			System.out.println("===----"+departmentID);
			System.out.println("===----"+start);
			System.out.println("===----"+limit);
			List<User> all = null ;
			QueryFilter filter = new QueryFilter() ;
			if(departmentID==0) {
				System.out.println("==========");
				filter.setPagingBean(new PagingBean(start,limit)) ;
				all = userService.getDao().getAll(filter);
			}else{
				
				filter.addFilter(new FieldBean("department.departmentID" ,departmentID ,Operation.EQUAL));
				filter.setPagingBean(new PagingBean(start,limit)) ;
				all = userService.getDao().getAll(filter);
			}
			JsonArray jarray = new JsonArray() ;
			JsonObject result = new JsonObject() ;
			for(User user : all) {
				JsonObject jobj = new JsonObject() ;
				jobj.addProperty("operatorCode", user.getOperatorCode()) ;
				jobj.addProperty("userName", user.getUserName()) ;
				jobj.addProperty("userAccount", user.getUserAccount()) ;
				jobj.addProperty("passWord", user.getPassWord());
				jobj.addProperty("stationName", user.getStation().getStationName()) ;
				jobj.addProperty("professName", user.getProfess().getProfessName()) ;
				jobj.addProperty("remarks", user.getRemarks()) ;
				jobj.addProperty("stationID", user.getStation().getStationID()) ;
				jobj.addProperty("professID", user.getProfess().getProfessID()) ;
				jobj.addProperty("userID", user.getUserID()) ;
				jarray.add(jobj) ;
			}
			result.addProperty("totalProperty",userService.getDao().getTotalProperty(filter)) ;
			result.add("result", jarray) ;
			result.addProperty("start", start);
			result.addProperty("limit", limit) ;
			this.jsonString = gson.toJson(result) ;
			System.out.println(jsonString) ;
			return SUCCESS ;
		}catch(Exception e){
			e.printStackTrace();
			return SUCCESS ;
		}
		
	}
	public String delete() {  //删除
		try{
			userService.removeService(userID) ;
			this.jsonString="{success:true}" ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		
		return SUCCESS ;
	}
	
	public String update() {
		try{
			userService.updateService(user) ;
			this.jsonString = "{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString = "{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String updatePwd() {
		try{
			User u = this.userService.loadService((Integer) this.session.get("userid"));
			u.setPassWord(this.password);
			this.userService.updateService(u);
			this.jsonString = "{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString = "{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String getUserInfo() {
		try{
			String userAccount = (String) session.get("useraccount");
			User user = this.userService.getUserByAccount(userAccount);
			if(user!=null) {
				JsonObject jobj = new JsonObject() ;
				jobj.addProperty("userid", String.valueOf(user.getUserID())) ;
				jobj.addProperty("useraccount", user.getUserAccount()) ;
				jobj.addProperty("username", user.getUserName()) ;
				Role role = user.getRoles().size()<1?null:(Role)(user.getRoles().toArray()[0]);
				jobj.addProperty("userrole", role==null?"":role.getRoleName()) ;
				jobj.addProperty("department", user.getDepartment().getDepName()) ;
				jobj.addProperty("success", true) ;
				this.jsonString=super.gson.toJson(jobj) ;
			} else {
				this.jsonString="{success:false}" ;
			}
			
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		System.out.println(this.jsonString);
		return SUCCESS ;
	}
}
